package com.chen.controller.front;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.chen.common.Constants;
import com.chen.common.Result;
import com.chen.entity.User;
import com.chen.entity.vo.UserDTO;
import com.chen.entity.vo.UserPasswordDTO;
import com.chen.exception.ServiceException;
import com.chen.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;

/**
 * @author Y·C
 * @version 1.0.0
 * @ClassName LoginControllerFront.java
 * @Description TODO
 * @createTime 2023年04月20日 08:40:00
 */

@Api(tags = "前台登录注册接口")
@RestController
@RequestMapping("/front")
public class LoginControllerFront {

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Resource
    private UserService userService;


    @ApiOperation("登录接口")
    @PostMapping("/login")
    public Result login (@RequestBody UserDTO userDTO){

        String username = userDTO.getUsername();
        String email = userDTO.getEmail();
        String password = userDTO.getPassword();
        if (StrUtil.isBlank(username)&&StrUtil.isBlank(email) || StrUtil.isBlank(password) ) {
            return Result.error(Constants.CODE_400, "参数错误");
        }
        UserDTO dto = userService.loginFront(userDTO);
        return Result.success(dto);

    }


    @ApiOperation("注册接口")
    @PostMapping("/register")
    public Result register (@RequestBody UserDTO userDTO){


        String code = redisTemplate.opsForValue().get(Constants.CAPTCHA_CODE_KEY + userDTO.getEmail());
        if (StringUtils.isEmpty(code)) {
            throw new ServiceException("2001","请获取验证码");
        }
        if (!code.equals(userDTO.getCode())) {
            throw new ServiceException("2001","邮箱验证码不对");
        }

        String username = userDTO.getUsername();
        String password = userDTO.getPassword();
        String email = userDTO.getEmail();
        if (StrUtil.isBlank(username) || StrUtil.isBlank(password) || StrUtil.isBlank(email)) {
            return Result.error(Constants.CODE_400, "参数错误");
        }
        return Result.success(userService.registerFront(userDTO));

    }


    @ApiOperation("修改密码接口")
    @PostMapping("/password")
    public Result password (@RequestBody UserPasswordDTO userPasswordDTO){


        String code = redisTemplate.opsForValue().get(Constants.CAPTCHA_CODE_KEY + userPasswordDTO.getEmail());
        if (StringUtils.isEmpty(code)) {
            throw new ServiceException("2001","请获取验证码");
        }
        if (!code.equals(userPasswordDTO.getCode())) {
            throw new ServiceException("2001","邮箱验证码不对");
        }

        userPasswordDTO.setPassword(SecureUtil.md5(userPasswordDTO.getPassword()));
        userPasswordDTO.setNewPassword(SecureUtil.md5(userPasswordDTO.getNewPassword()));
        userService.updatePasswordFront(userPasswordDTO);

        return Result.success();
    }


    @ApiOperation("完善、修改个人基础信息接口")
    @PostMapping("/updateInfo")
    public Result updatePersonalInfo(@RequestBody User user){

        userService.manageInfo(user);
        return Result.success();
    }


}
